package face.pay.cpn.cpnmachServiceinfo.impl;


import face.pay.cpn.cpnmachServiceinfo.ICpnMchServiceInfoService;
import face.pay.dao.CpnMchServiceInfoDao;
import face.pay.dao.CpnMchWorkTimeDao;
import face.pay.entity.CpnMchServiceInfo;
import face.pay.entity.CpnMchWorkTime;
import face.pay.entity.UserInfo;
import face.pay.util.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class CpnMchServiceInfoServiceimpl implements ICpnMchServiceInfoService {

    @Autowired
    private CpnMchServiceInfoDao cpnMchServiceInfoDao;


    @Autowired
    private CpnMchWorkTimeDao cpnMchWorkTimeDao;


    static final String[] workTimes = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"};

    /**
     * 查找商家制券服务信息
     *
     * @param loginuser
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CpnMchServiceInfo querycpnServiceInfo(UserInfo loginuser) {
        CpnMchServiceInfo record = new CpnMchServiceInfo();
        record.setUserId(loginuser.getId());
        CpnMchServiceInfo info = this.cpnMchServiceInfoDao.queryCpnMchServiceInfo(record);
        if (info == null) {
            CpnMchServiceInfo cpnMchServiceInfo = new CpnMchServiceInfo();
            cpnMchServiceInfo.setUserId(loginuser.getId());
            cpnMchServiceInfo.setOemId(loginuser.getOemId());
            cpnMchServiceInfo.setCreatedBy(loginuser.getId());
            cpnMchServiceInfo.setUpdatedBy(loginuser.getId());
            this.cpnMchServiceInfoDao.insert(cpnMchServiceInfo);
            //创建默认的商家七天工作时间
            creatDefaultWorkTime(loginuser);
            return cpnMchServiceInfo;
        } else {
            return info;
        }
    }

    private void creatDefaultWorkTime(UserInfo loginuser) {
        ArrayList<CpnMchWorkTime> workList = new ArrayList<>();
        for (String time : workTimes) {
            CpnMchWorkTime wk = new CpnMchWorkTime();
            wk.setOemId(loginuser.getOemId());
            wk.setMch_user_id(loginuser.getId());
            wk.setType(time);
            wk.setBegin_hour("09");
            wk.setBegin_minute("00");
            wk.setEnd_hour("21");
            wk.setEnd_minute("00");
            wk.setStatus(1);
            wk.setCreatedBy(loginuser.getId());
            wk.setUpdatedBy(loginuser.getId());
            workList.add(wk);
        }
        this.cpnMchWorkTimeDao.insertDefaultWorkTime(workList);
    }


    /**
     * 更新商家服务信息
     *
     * @param loginUser
     * @param cpnMchServiceInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage ediet(UserInfo loginUser, CpnMchServiceInfo cpnMchServiceInfo) {
        cpnMchServiceInfo.setUpdatedBy(loginUser.getId());
        this.cpnMchServiceInfoDao.update(cpnMchServiceInfo);
        return ResultMessage.createSuccess();
    }


    /**
     * 查询商家工作时间
     *
     * @param loginUser
     * @return
     */
    @Override
    public List<CpnMchWorkTime> queryMchWorkTime(UserInfo loginUser) {
        CpnMchWorkTime record = new CpnMchWorkTime();
        record.setMch_user_id(loginUser.getId());
        List<CpnMchWorkTime> list = this.cpnMchWorkTimeDao.queryCpnMchWorkTime(record);
        return list;
    }

    /**
     * 修改商家工作时
     *
     * @param loginUser
     * @param workTimes
     * @return
     */
    @Override
    public ResultMessage updataWorkTime(UserInfo loginUser, List<CpnMchWorkTime> workTimes) {
        for (CpnMchWorkTime workTime : workTimes) {
            this.cpnMchWorkTimeDao.update(workTime);
        }
        return ResultMessage.createSuccess();
    }
}
